# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render, redirect, reverse
from django.template import loader

from .models import BookInfo, HreoInfo, Student


def index(request):
    # 获取模版对象
    res = loader.get_template('index.html')
    # 添加数据
    result = res.render({'name': '张三'})
    return HttpResponse(result)


def list(request):
    # 获取图书信息返回到列表页面
    books = BookInfo.objects.all()
    res = loader.get_template('list.html')
    result = res.render({'books': books})
    return HttpResponse(result)


def detail(request, id):
    # 获取数据库中单体记录的数据
    books = BookInfo.objects.all()
    print(books)
    book = BookInfo.objects.get(id=id)
    print(book)
    heros = book.hreoinfo_set.all()
    context = {'book': book, 'heros': heros}
    # hreos = HreoInfo.objects.all()
    # print(hreos)
    # hreo = HreoInfo.objects.get(id=id)
    # print(hreo)

    # return HttpResponse("这是尾页%s" % id)
    return render(request, 'detall.html', context)


def delhero(req, id):
    # 根据id删除数据
    hero = HreoInfo.objects.get(pk=id)
    book = hero.book
    # heros = book.hreoinfo_set.all()
    # 删除
    hero.delete()
    # context = {'book': book, 'heros': heros}
    # return render(req, 'bookteat/detail.html', context)
    # print(reverse('bookdetail', args=(book.id)))
    return redirect(f'/bookteat/detail/{book.id}')


def delbook(req, id):
    book = BookInfo.objects.get(pk=id)
    heros = book.hreoinfo_set.all()
    if len(heros) > 0:
        for hero in heros:
            hero.delete()
    book.delete()
    # 反向解析
    # print(reverse('booklist'))
    # 重定向
    return redirect('/bookteat/list')


def addbook(req):
    if req.method == 'GET':
        return render(req, 'addbook.html')
    elif req.method == 'POST':
        # 获取form表单中的数据，name属性获取值
        title = req.POST.get('title')
        pub_date = req.POST.get('pub_date')
        bookInfo = BookInfo()
        bookInfo.title = title
        bookInfo.pub_date = pub_date
        bookInfo.save()
        return redirect(reverse('booklist'))


def stulist(req):
    datas = Student.objects.order_by('-age')
    for data in datas:
        print(data.name, data.age)
    stus = Student.objects.all()
    for stu in stus:
        print(stu.name, stu.age, stu.sex)
    print('objects:', Student.objects)
    print('count()统计查询结果的条数：',Student.objects.count())
    print('first()返回第一个对象',Student.objects.first().id)
    print('last()返回最后一天记录',Student.objects.last())
    print('exists():判断结果集是否为空：',Student.objects.exists())
    # filter 过滤器
    ss = Student.objects.filter(id__exact=3)
    print(ss.query)
    stus = Student.objects.all()[0:2]
    for stu in stus:
        print(stu)
    data = Student.objects.exclude(name__contains='admin')
    print(data.query)
    data = Student.objects.filter(name__startswith='ad')
    print(data.query)
    data = Student.objects.filter(age__in=[17,18,20])
    print(data.query)
    data = Student.objects.filter(age__lt=18)
    print(data)
    print(data.query)
    return HttpResponse('获取学生信息')
